SpringBoot官方文档翻译(十五):配置类

15. Configuration Classes(配置类)

1
2
3
4
5
Spring Boot favors Java-based configuration. Although it is     
possible to use SpringApplication with XML sources, we generally
recommend that your primary source be a single @Configuration class.
Usually the class that defines the main method is a good candidate
as the primary @Configuration.

Spring Boot偏向于使用Java-base的配置方式。尽管他也能够支持使用SpringApplication配合XML,我们通常还是强烈建议您使用单个的@Configuration注解类做配置类。通常定义main方法的类用来作为一个主配置类是一个很好的选择。

15.1 Importing Additional Configuration Classes(导入其他配置类)

1
2
3
4
5
You need not put all your @Configuration into     
a single class. The @Import annotation can be
used to import additional configuration classes.
Alternatively, you can use @ComponentScan to automatically
pick up all Spring components, including @Configuration classes.

您并不需要将您所有的配置放在一个@Configuration注解的类中。@Import注解可以用来导入额外的注解类。另外,您可以使用@ComponentScan注解去制动选择所有的Spring组件。包括@Configuration注解的配置类。

1
2
3
We recommend that you follow Java’s recommended package naming     
conventions and use a reversed domain name (for example, 
com.example.project).

我们建议您遵循Java推荐的软件包命名约定并使用反向域名(例如,com.example.project)。

15.2 Importing XML Configuration(导入XML配置)

1
2
3
If you absolutely must use XML based configuration, we recommend     
that you still start with a @Configuration class. You can then
use an @ImportResourceannotation to load XML configuration files.

如果您是在要使用基于XML的配置,我们建议您还是从一个@Configuration注解的类开始。您可以在其中使用@ImportResourceannotation注解去加载XML配置文件。

分享到